
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>Unix syslog 库例程 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="被取代的模块" href="superseded.html" />
    <link rel="prev" title="nis --- Sun 的 NIS (黄页) 接口" href="nis.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/syslog.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="superseded.html" title="被取代的模块"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="nis.html" title="nis --- Sun 的 NIS (黄页) 接口"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="unix.html" accesskey="U">Unix 专有服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-syslog">
<span id="syslog-unix-syslog-library-routines"></span><h1>Unix syslog 库例程<a class="headerlink" href="#module-syslog" title="永久链接至标题">¶</a></h1>
<hr class="docutils" />
<p>此模块提供一个接口到Unix <code class="docutils literal notranslate"><span class="pre">syslog</span></code> 日常库. 参考 Unix 手册页关于 <code class="docutils literal notranslate"><span class="pre">syslog</span></code> 设施的详细描述.</p>
<p>此模块包装了系统的 <code class="docutils literal notranslate"><span class="pre">syslog</span></code> 例程族。 一个能与 syslog 服务器对话的纯 Python 库则以 <a class="reference internal" href="logging.handlers.html#module-logging.handlers" title="logging.handlers: Handlers for the logging module."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.handlers</span></code></a> 模块中 <code class="xref py py-class docutils literal notranslate"><span class="pre">SysLogHandler</span></code> 类的形式提供。</p>
<p>这个模块定义了以下函数：</p>
<dl class="function">
<dt id="syslog.syslog">
<code class="sig-prename descclassname">syslog.</code><code class="sig-name descname">syslog</code><span class="sig-paren">(</span><em class="sig-param">message</em><span class="sig-paren">)</span><a class="headerlink" href="#syslog.syslog" title="永久链接至目标">¶</a></dt>
<dt>
<code class="sig-prename descclassname">syslog.</code><code class="sig-name descname">syslog</code><span class="sig-paren">(</span><em class="sig-param">priority</em>, <em class="sig-param">message</em><span class="sig-paren">)</span></dt>
<dd><p>将字符串 <em>message</em> 发送到系统日志记录器。 如有必要会添加末尾换行符。 每条消息都带有一个由 <em>facility</em> 和 <em>level</em> 组成的优先级标价签。 可选的 <em>priority</em> 参数默认值为 <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_INFO</span></code>，它确定消息的优先级。 如果未在 <em>priority</em> 中使用逻辑或 (<code class="docutils literal notranslate"><span class="pre">LOG_INFO</span> <span class="pre">|</span> <span class="pre">LOG_USER</span></code>) 对 facility 进行编码，则会使用在 <a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal notranslate"><span class="pre">openlog()</span></code></a> 调用中所给定的值。</p>
<p>如果 <a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal notranslate"><span class="pre">openlog()</span></code></a> 未在对 <a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-func docutils literal notranslate"><span class="pre">syslog()</span></code></a> 的调用之前被调用，则将不带参数地调用 <code class="docutils literal notranslate"><span class="pre">openlog()</span></code>。</p>
</dd></dl>

<dl class="function">
<dt id="syslog.openlog">
<code class="sig-prename descclassname">syslog.</code><code class="sig-name descname">openlog</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">ident</em><span class="optional">[</span>, <em class="sig-param">logoption</em><span class="optional">[</span>, <em class="sig-param">facility</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#syslog.openlog" title="永久链接至目标">¶</a></dt>
<dd><p>后续 <a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-func docutils literal notranslate"><span class="pre">syslog()</span></code></a> 调用的日志选项可以通过调用 <a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal notranslate"><span class="pre">openlog()</span></code></a> 来设置。 如果日志当前未打开则 <a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-func docutils literal notranslate"><span class="pre">syslog()</span></code></a> 将不带参数地调用 <a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal notranslate"><span class="pre">openlog()</span></code></a>。</p>
<p>可选的 <em>ident</em> 关键字参数是在每条消息前添加的字符串，默认为 <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> 去除打头的路径部分。 可选的 <em>logoption</em> 关键字参数（默认为 0）是一个位字段 -- 请参见下文了解可能的组合值。 可选的 <em>facility</em> 关键字参数 (默认为 <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_USER</span></code>) 为没有显式编码 facility 的消息设置默认的 facility。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>在之前的版本中，不允许关键字参数，并且要求必须有 <em>ident</em>。 <em>ident</em> 的默认值依赖于系统库，它往往为 <code class="docutils literal notranslate"><span class="pre">python</span></code> 而不是 Python 程序文件的实际名称。</p>
</div>
</dd></dl>

<dl class="function">
<dt id="syslog.closelog">
<code class="sig-prename descclassname">syslog.</code><code class="sig-name descname">closelog</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#syslog.closelog" title="永久链接至目标">¶</a></dt>
<dd><p>重置日志模块值并且调用系统库 <code class="docutils literal notranslate"><span class="pre">closelog()</span></code>.</p>
<p>这使得此模块在初始导入时行为固定。 例如，<a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal notranslate"><span class="pre">openlog()</span></code></a> 将在首次调用 <a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-func docutils literal notranslate"><span class="pre">syslog()</span></code></a> 时被调用（如果 <a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal notranslate"><span class="pre">openlog()</span></code></a> 还未被调用过），并且 <em>ident</em> 和其他 <a class="reference internal" href="#syslog.openlog" title="syslog.openlog"><code class="xref py py-func docutils literal notranslate"><span class="pre">openlog()</span></code></a> 形参会被重置为默认值。</p>
</dd></dl>

<dl class="function">
<dt id="syslog.setlogmask">
<code class="sig-prename descclassname">syslog.</code><code class="sig-name descname">setlogmask</code><span class="sig-paren">(</span><em class="sig-param">maskpri</em><span class="sig-paren">)</span><a class="headerlink" href="#syslog.setlogmask" title="永久链接至目标">¶</a></dt>
<dd><p>将优先级掩码设为 <em>maskpri</em> 并返回之前的掩码值。 调用 <a class="reference internal" href="#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-func docutils literal notranslate"><span class="pre">syslog()</span></code></a> 并附带未在 <em>maskpri</em> 中设置的优先级将会被忽略。 默认设置为记录所有优先级。 函数 <code class="docutils literal notranslate"><span class="pre">LOG_MASK(pri)</span></code> 可计算单个优先级 <em>pri</em> 的掩码。 函数 <code class="docutils literal notranslate"><span class="pre">LOG_UPTO(pri)</span></code> 可计算包括 <em>pri</em> 在内的所有优先级的掩码。</p>
</dd></dl>

<p>此莫款定义了一下常量:</p>
<dl class="simple">
<dt>优先级级别 (高到低):</dt><dd><p><code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_EMERG</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_ALERT</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_CRIT</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_ERR</span></code>,
<code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_WARNING</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_NOTICE</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_INFO</span></code>,
<code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_DEBUG</span></code>.</p>
</dd>
<dt>设施:</dt><dd><p><code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_KERN</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_USER</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_MAIL</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_DAEMON</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_AUTH</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_LPR</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_NEWS</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_UUCP</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_CRON</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_SYSLOG</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_LOCAL0</span></code> to <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_LOCAL7</span></code>，如果 <code class="docutils literal notranslate"><span class="pre">&lt;syslog.h&gt;</span></code> 中有定义则还有 <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_AUTHPRIV</span></code>。</p>
</dd>
<dt>日志选项:</dt><dd><p><code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_PID</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_CONS</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_NDELAY</span></code>，如果 <code class="docutils literal notranslate"><span class="pre">&lt;syslog.h&gt;</span></code> 中有定义则还有 <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_ODELAY</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_NOWAIT</span></code> 以及 <code class="xref py py-const docutils literal notranslate"><span class="pre">LOG_PERROR</span></code>。</p>
</dd>
</dl>
<div class="section" id="examples">
<h2>例子<a class="headerlink" href="#examples" title="永久链接至标题">¶</a></h2>
<div class="section" id="simple-example">
<h3>简单示例<a class="headerlink" href="#simple-example" title="永久链接至标题">¶</a></h3>
<p>一个简单的示例集:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">syslog</span>

<span class="n">syslog</span><span class="o">.</span><span class="n">syslog</span><span class="p">(</span><span class="s1">&#39;Processing started&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">error</span><span class="p">:</span>
    <span class="n">syslog</span><span class="o">.</span><span class="n">syslog</span><span class="p">(</span><span class="n">syslog</span><span class="o">.</span><span class="n">LOG_ERR</span><span class="p">,</span> <span class="s1">&#39;Processing started&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>一个设置多种日志选项的示例，其中有在日志消息中包含进程 ID，以及将消息写入用于邮件日志记录的目标设施等:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">syslog</span><span class="o">.</span><span class="n">openlog</span><span class="p">(</span><span class="n">logoption</span><span class="o">=</span><span class="n">syslog</span><span class="o">.</span><span class="n">LOG_PID</span><span class="p">,</span> <span class="n">facility</span><span class="o">=</span><span class="n">syslog</span><span class="o">.</span><span class="n">LOG_MAIL</span><span class="p">)</span>
<span class="n">syslog</span><span class="o">.</span><span class="n">syslog</span><span class="p">(</span><span class="s1">&#39;E-mail processing initiated...&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#">Unix syslog 库例程</a><ul>
<li><a class="reference internal" href="#examples">例子</a><ul>
<li><a class="reference internal" href="#simple-example">简单示例</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="nis.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">nis</span></code> --- Sun 的 NIS (黄页) 接口</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="superseded.html"
                        title="下一章">被取代的模块</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/syslog.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="superseded.html" title="被取代的模块"
             >下一页</a> |</li>
        <li class="right" >
          <a href="nis.html" title="nis --- Sun 的 NIS (黄页) 接口"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="unix.html" >Unix 专有服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>